package TUILayer;

import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;
/**
 * Write a description of class MainMenu here.
 * 
 * @author (Group A) 
 * @version (2014.11.03)
 */
public class PersonUI
{
    private PersonHandler people;

    public PersonUI()
    {
        people = new PersonHandler();
    }

    public void personMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writePersonMenu();

            switch (choise) 
            {
                case 1: startCreatePerson();
                break;
                case 2: startFindPerson();
                break;
                case 3: startUpdatePerson();
                break;
                case 4: startDeletePerson();
                break;
                default: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writePersonMenu()
    {
        int choise = 0;
        try
        {
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Person ***");
            System.out.println(" (1) Add Person");
            System.out.println(" (2) Find Person");
            System.out.println(" (3) Change Person Information");
            System.out.println(" (4) Delete Person");
            System.out.println(" (5) Back");
            System.out.print("\n Choose an option: ");

            choise = keyboard.nextInt();       
        } 
        catch(Exception e) 
        {
            System.out.println("ERROR!");
            pause();
            personMenuStart();
        }
        return choise;
    }

    private void startCreatePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        try 
        {
            System.out.println("Fill in the information of the new person: ");
            System.out.println("ID:");
            int id = Integer.parseInt(keyboard.nextLine());
            System.out.println("Name:");
            String name = keyboard.nextLine();
            System.out.println("Address:");
            String address = keyboard.nextLine();
            System.out.println("Postal code:");
            int postalCode = Integer.parseInt(keyboard.nextLine());
            System.out.println("City:");
            String city = keyboard.nextLine();
            System.out.println("Phone number:");
            String phone = keyboard.nextLine();
            Person person = new Person(id,name,address,postalCode,city,phone);
            if(people.addPerson(person))
            {
                people.addPerson(person);
                System.out.println("A new person has been added!");
            } 
            else 
            {
                System.out.println("A person with the same ID already exists! Try another ID!");
            }
        } 
        catch (Exception e) 
        {
            System.out.println("Something went wrong!");
        }
        pause();
    }

    private void startFindPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the person you are looking for:");
        try
        {
            int id = keyboard.nextInt();
            Person foundPerson = people.findPerson(id);
            System.out.println("ID: " + foundPerson.getPersonID());
            System.out.println("Name: " + foundPerson.getName());
            System.out.println("Address: " + foundPerson.getAddress());
            System.out.println("Postal Code: " + foundPerson.getPostalCode());
            System.out.println("City: " + foundPerson.getCity());
            System.out.println("Phone: " + foundPerson.getPhone());
        } 
        catch (Exception e) 
        {
            System.out.println("The person you are looking for can not be found!");
        }
        pause();
    }

    private void startUpdatePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            System.out.println("Enter the ID of the person:");
            int id = Integer.parseInt(keyboard.nextLine());
            System.out.println("Fill in the new information:");
            System.out.println("Name:");
            String name = keyboard.nextLine();
            System.out.println("Address:");
            String address = keyboard.nextLine();
            System.out.println("Postal code:");
            int postalCode = Integer.parseInt(keyboard.nextLine());
            System.out.println("City:");
            String city = keyboard.nextLine();
            System.out.println("Phone number: ");
            String phone = keyboard.nextLine();
            people.updatePerson(id,name,address,postalCode,city,phone);
            System.out.println("The information has been updated!");
        }
        catch(Exception e) 
        {
            System.out.println("Error! Try again");
        }
        pause();
    }

    public void startDeletePerson()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the ID of the person you want to delete:");
            int id = keyboard.nextInt();
            people.deletePerson(id);    
        } 
        catch (Exception e) 
        {
            System.out.println("The person you are looking for can not be found!");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}